package com.fun.feifun.modules.business.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fun.feifun.base.utils.common.ListUtil;
import com.fun.feifun.modules.business.model.BusinessGoodsSupplierSort;
import com.fun.feifun.modules.business.model.BusinessSupplier;
import com.fun.feifun.modules.business.model.BusinessUserGoods;
import com.fun.feifun.modules.business.model.dto.BusinessGoodsDto;
import com.fun.feifun.modules.business.model.dto.BusinessGoodsRouteDto;
import com.fun.feifun.modules.business.model.dto.BusinessGoodsSave;
import com.fun.feifun.modules.business.model.param.BusinessGoodsPage;
import com.fun.feifun.modules.business.model.param.BusinessGoodsOperation;
import com.fun.feifun.modules.business.model.param.BusinessGoodsRoute;
import com.fun.feifun.modules.business.model.param.BusinessUserGoodsParam;
import com.fun.feifun.modules.business.service.IBusinessGoodsSupplierSortService;
import com.fun.feifun.modules.business.service.IBusinessSupplierService;
import com.fun.feifun.modules.business.service.IBusinessUserGoodsService;
import com.fun.feifun.modules.sys.auth.model.SysMenu;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
import com.fun.feifun.modules.business.service.IBusinessGoodsService;
import com.fun.feifun.modules.business.model.BusinessGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fun.feifun.base.result.Result;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务商品 前端控制器
 * </p>
 *
 * @author wkf
 * @since 2024-05-29
 */
@RestController
@Api(value = "/business/business-goods", tags = "业务商品")
@Slf4j
@SaCheckLogin()
@RequestMapping("/business/business-goods")
public class BusinessGoodsController {


    @Autowired
    private IBusinessGoodsService service;

    @Autowired
    private IBusinessUserGoodsService userGoodsSevice;

    @Resource
    private IBusinessSupplierService supplierService;

    @Resource
    private IBusinessGoodsService goodsService;

    @Autowired
    private IBusinessGoodsSupplierSortService goodsSupplierSortService ;
    /**
     * 获取分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "获取分页信息")
    public Result<Page<BusinessGoodsDto>> page(@Validated @RequestBody BusinessGoodsPage param){
        Page page = new Page(param.getPage(),param.getSize());
        QueryWrapper<BusinessGoods> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param.getCode())){
            wrapper.lambda().like(BusinessGoods::getCode,param.getCode());
        }
        if(StringUtils.isNotEmpty(param.getGoodsName())){
            wrapper.lambda().like(BusinessGoods::getTitle,param.getGoodsName());
        }
        if(StringUtils.isNotEmpty(param.getType())){
            wrapper.lambda().eq(BusinessGoods::getType,param.getType());
        }
        if(StringUtils.isNotEmpty(param.getOperators())){
            wrapper.lambda().eq(BusinessGoods::getOperators,param.getOperators());
        }
        if(param.getIsValid()!=null){
            wrapper.lambda().eq(BusinessGoods::getIsValid,param.getIsValid());
        }
        wrapper.lambda().eq(BusinessGoods::getDelFlag,false);
        service.page(page,wrapper);
        List<BusinessGoods> list = page.getRecords();
        List<BusinessGoodsDto> records =list.stream().map(it->it.toDto()).collect(Collectors.toList());
        page.setRecords(records);
        return Result.success(page);
    }


    /**
     * 添加数据
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "添加或者保存")
    public Result save(@RequestBody BusinessGoodsSave item){

        QueryWrapper<BusinessGoods> wrapper=new QueryWrapper();
        wrapper.lambda().eq(BusinessGoods::getCode,item.getCode());
        BusinessGoods one = service.getOne(wrapper);
        if(item.getId()==null&& ObjectUtils.isNotEmpty(one)){
            return Result.err("添加商品code重复，添加失败！");
        }
        if(item.getId()!=null&&ObjectUtils.isNotEmpty(one)&&!one.getId().equals(item.getId())){
            return Result.err("修改商品code重复，修改失败！");
        }
        if(service.saveOrUpdate(item)){
            return Result.success("数据添加成功！");
        }
        return Result.err("数据添加成功！");
    }



    /**
     * 删除数据
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "删除")
    public Result deleteItems(@RequestBody List<Long> ids){
        QueryWrapper<BusinessUserGoods> userGoodsWrapper=new QueryWrapper();
        userGoodsWrapper.lambda().in(BusinessUserGoods::getGoodsId,ids);
        int count = userGoodsSevice.count(userGoodsWrapper);
        if(count>0){
            return Result.err("目前商品数据正在被代理用户使用无法删除。");
        }
        UpdateWrapper<BusinessGoods> wrapper=new UpdateWrapper();
        wrapper.lambda().set(BusinessGoods::getDelFlag,true).in(BusinessGoods::getId,ids);
        if(service.update(wrapper)){
            return Result.success("数据删除成功！");
        }
        return Result.err("数据删除失败");
    }


    /**
     * 根据ID查找数据
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查找")
    public Result findById(@RequestParam("id") Long id){
        BusinessGoods item = service.getById(id);
        if(item==null){
            return Result.err("尚未查询到此ID");
        }
        return Result.success(item);
    }


    @PostMapping("/operationGoods")
    @ApiOperation(value = "操作商品上下架")
    public Result operationGoods(@RequestBody BusinessGoodsOperation param){
        UpdateWrapper<BusinessGoods> wrapper=new UpdateWrapper<>();
        wrapper.lambda().in(BusinessGoods::getId,param.getIds()).set(BusinessGoods::getIsValid,param.getIsValid());
        boolean update = service.update(wrapper);
        if(update){
            return Result.success("上架成功");
        }
        return Result.err("上架失败");
    }


    @GetMapping("/allUp")
    @ApiOperation(value = "获取所有上架商品")
    public Result< List<BusinessGoods>> getAll(){
        QueryWrapper<BusinessGoods> wrapper=new QueryWrapper();
        wrapper.lambda().eq(BusinessGoods::getIsValid,true);
        List<BusinessGoods> all = service.list(wrapper);
        return Result.success(all);
    }

}

